﻿using SuperFx;
using SuperLottery.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Globalization;

namespace LotteryCrawler.Core
{
	/// <summary>
	/// 
	/// </summary>
	public class Crawler
	{
		/// <summary>
		/// 抓取双色球
		/// </summary>
		/// <param name="issueNumber"></param>
		/// <returns></returns>
		public DrawResult CrawlShuangSeQiu(string issueNumber)
		{
			DrawResult model = new DrawResult();
			model.Draw = new LotteryDraw();
			model.Draw.Issuer = "福彩";
			model.Draw.Game = "双色球";
			model.Draw.IssueNumber = issueNumber;

			string detailUrl = "http://www.chinafcw.com.cn/ulWinInfo.do?issueNumber={0}&method=detail";
			detailUrl = String.Format(detailUrl, issueNumber);
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			string drawDatePattern = @"<td width=""13%"" height=""36"" class=""leftfont"">开奖日期：</td>\s*<td width=""87%"" class=""leftfont"">(\d{4}-\d{1,2}-\d{1,2})";
			if (!Regex.IsMatch(htmlSource, drawDatePattern))
			{
				throw new Exception("未抓取到数据");
			}
			var match = Regex.Match(htmlSource, drawDatePattern, RegexOptions.IgnoreCase);
			string strDrawDate = match.Groups[1].Value;
			DateTime drawDate;
			DateTime.TryParse(strDrawDate, out drawDate);
			model.Draw.DrawDate = drawDate;

			string numbersPattern = @"<td height=""36"" class=""leftfont"">红球号码：</td>[\s]*<td class=""kj_img""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""><img src=""/images/num/(\d{2})\.jpg"" width=""38"" height=""28""></td>";
			match = Regex.Match(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			string numbers = match.Groups.OfType<Group>().Skip(1).Select(group => group.Value).Aggregate((a, b) => a + "," + b);
			model.Draw.Numbers = numbers;

			string bonusNumbersPattern = @"<td height=""36"" class=""leftfont"">蓝球号码：</td>\s*<td class=""kj_img""><img src=""/images/num/b(\d{2}).jpg"" width=""38"" height=""28""></td>";
			match = Regex.Match(htmlSource, bonusNumbersPattern, RegexOptions.IgnoreCase);
			string bonusNumbers = match.Groups[1].Value;
			model.Draw.BonusNumbers = bonusNumbers;

			string amountPattern = @"本期投注金额为\s*(?<SalesAmount>\d*)\s*<br>\W*奖池资金累计为\s*(?<CumulativeAmount>\d*)\s*元";
			match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
			string strSalesAmount = match.Groups["SalesAmount"].Value;
			string strCumulativeAmount = match.Groups["CumulativeAmount"].Value;
			long salesAmount, cumulativeAmount;
			Int64.TryParse(strSalesAmount, out salesAmount);
			model.Draw.SalesAmount = salesAmount;
			Int64.TryParse(strCumulativeAmount, out cumulativeAmount);
			model.Draw.CumulativeAmount = cumulativeAmount;

			string winningDetailPattern = @"(?<=>)(?<Award>\w{2,}奖)(?=<\/td>)[\s\S]*?(?<=>)(?<NumberOfWinners>\d*?)(?=<\/td>)[\s\S]*?(?<=>)(?<MoneyPerWinner>\d*?)(?=<\/td>)";
			MatchCollection matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;
				WinningDetail detail = new WinningDetail();
				detail.Serial = serial;
				detail.Award = matchItem.Groups["Award"].Value;
				string strNumberOfWinners = matchItem.Groups["NumberOfWinners"].Value;
				string strMoneyPerWinner = matchItem.Groups["MoneyPerWinner"].Value;
				int numberOfWinners, moneyPerWinner;
				Int32.TryParse(strNumberOfWinners, out numberOfWinners);
				detail.NumberOfWinners = numberOfWinners;
				Int32.TryParse(strMoneyPerWinner, out moneyPerWinner);
				detail.MoneyPerWinner = moneyPerWinner;

				if (detail.Award == "快乐星期天特别奖" && detail.NumberOfWinners == 0)
				{
					continue;
				}

				details.Add(detail);
			}
			model.Details = details;
			return model;
		}

		/// <summary>
		/// 从500万网站抓取双色球数据.
		/// </summary>
		/// <param name="issueNumber">期号.</param>
		/// <returns></returns>
		public DrawResult CrawlShuangSeQiuFrom500(string issueNumber)
		{
			DrawResult model = new DrawResult();
			model.Draw = new LotteryDraw();
			model.Draw.Issuer = "福彩";
			model.Draw.Game = "双色球";
			model.Draw.IssueNumber = issueNumber;

			string issueNumber5Bit = issueNumber.Substring(2, 5);

			// 数据来源URL
			string detailUrl = "http://kaijiang.500.com/shtml/ssq/{0}.shtml";
			detailUrl = String.Format(detailUrl, issueNumber5Bit);

			// 下载数据.
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			// 截取开奖日期.
			string drawDatePattern = @"<span class=""span_right"">开奖日期：((\d{4})年(\d{1,2})月(\d{1,2})日) 兑奖截止日期";
			if (!Regex.IsMatch(htmlSource, drawDatePattern))
			{
				throw new Exception("未抓取到开奖日期");
			}
			var match = Regex.Match(htmlSource, drawDatePattern, RegexOptions.IgnoreCase);
			string strDrawDate = match.Groups[1].Value;
			DateTime drawDate;
			DateTime.TryParse(strDrawDate, out drawDate);
			model.Draw.DrawDate = drawDate;

			// 截取红球与篮球数据.
			string numbersPattern = @"<div class=""ball_box01"">[\s]*<ul>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_blue"">(\d{2})</li>[\s]*</ul>[\s]*</div>";
			match = Regex.Match(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			// 红球.
			model.Draw.Numbers = match.Groups.OfType<Group>().Skip(1).Take(6).Select(group => group.Value).Aggregate((a, b) => a + "," + b);
			// 篮球.
			model.Draw.BonusNumbers = match.Groups.OfType<Group>().Skip(7).Take(1).Single().Value;

			try
			{
				// 本期销量
				string amountPattern = @"<td>[\s]*本期销量：<span[\s]*class=""cfont1[\s]*"">([\S]*?)元</span>&nbsp;&nbsp;&nbsp;&nbsp;奖池滚存：<span[\s]*class=""cfont1[\s]*"">([\S]*?)元</span>&nbsp;&nbsp;</td>";
				match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
				string strSalesAmount = match.Groups[1].Value;
				string strCumulativeAmount = match.Groups[2].Value;
				long salesAmount, cumulativeAmount;
				Int64.TryParse(strSalesAmount, NumberStyles.AllowThousands, null, out salesAmount);
				model.Draw.SalesAmount = salesAmount;
				Int64.TryParse(strCumulativeAmount, NumberStyles.AllowThousands, null, out cumulativeAmount);
				model.Draw.CumulativeAmount = cumulativeAmount;
			}
			catch
			{

			}

			string winningDetailPattern = @"<tr align=""center"">[\s]*<td>[\s]*([\S]*?)</td>[\s]*<td>[\s]*([\S]*?)</td>[\s]*<td>[\s]*([\S]*?)</td>[\s]*</tr>";
			MatchCollection matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;
				WinningDetail detail = new WinningDetail();
				detail.Serial = serial;
				detail.Award = matchItem.Groups[1].Value;
				string strNumberOfWinners = matchItem.Groups[2].Value;
				string strMoneyPerWinner = matchItem.Groups[3].Value;
				int numberOfWinners, moneyPerWinner;
				Int32.TryParse(strNumberOfWinners, out numberOfWinners);
				detail.NumberOfWinners = numberOfWinners;
				Int32.TryParse(strMoneyPerWinner, NumberStyles.AllowThousands, null, out moneyPerWinner);
				detail.MoneyPerWinner = moneyPerWinner;

				if (detail.Award == "快乐星期天特别奖" && detail.NumberOfWinners == 0)
				{
					continue;
				}

				details.Add(detail);
			}
			model.Details = details;
			return model;
		}

		/// <summary>
		/// 抓取七乐彩
		/// </summary>
		/// <param name="issueNumber"></param>
		/// <returns></returns>
		public DrawResult CrawlQiLeCai(string issueNumber)
		{
			DrawResult model = new DrawResult();
			model.Draw = new LotteryDraw();
			model.Draw.Issuer = "福彩";
			model.Draw.Game = "七乐彩";
			model.Draw.IssueNumber = issueNumber;

			string detailUrl = "http://www.chinafcw.com.cn/qlcWinInfo.do?issueNumber={0}&method=detail";
			detailUrl = String.Format(detailUrl, issueNumber);
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			string drawDatePattern = @"<td width=""13%"" height=""36"" class=""leftfont"">开奖日期：</td>\s*<td width=""87%"" class=""leftfont"">(\d{4}-\d{1,2}-\d{1,2})";
			if (!Regex.IsMatch(htmlSource, drawDatePattern))
			{
				throw new Exception("未抓取到数据");
			}
			var match = Regex.Match(htmlSource, drawDatePattern, RegexOptions.IgnoreCase);
			string strDrawDate = match.Groups[1].Value;
			DateTime drawDate;
			DateTime.TryParse(strDrawDate, out drawDate);
			model.Draw.DrawDate = drawDate;

			string numbersPattern = @"/images/num/(\d{2})\.jpg";
			var matches = Regex.Matches(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			string numbers = matches.OfType<Match>().Select(pMatch => pMatch.Groups[1].Value).Aggregate((a, b) => a + "," + b);
			model.Draw.Numbers = numbers;

			string bonusNumbersPattern = @"/images/num/b(\d{2})\.jpg";
			match = Regex.Match(htmlSource, bonusNumbersPattern, RegexOptions.IgnoreCase);
			string bonusNumbers = match.Groups[1].Value;
			model.Draw.BonusNumbers = bonusNumbers;

			string amountPattern = @"本期投注金额为\s*(?<SalesAmount>\d*)\s*<br>\W*奖池资金累计为\s*(?<CumulativeAmount>\d*)\s*元";
			match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
			string strSalesAmount = match.Groups["SalesAmount"].Value;
			string strCumulativeAmount = match.Groups["CumulativeAmount"].Value;
			long salesAmount, cumulativeAmount;
			Int64.TryParse(strSalesAmount, out salesAmount);
			model.Draw.SalesAmount = salesAmount;
			Int64.TryParse(strCumulativeAmount, out cumulativeAmount);
			model.Draw.CumulativeAmount = cumulativeAmount;

			string winningDetailPattern = @"(?<=>)(?<Award>\w{2,}奖)(?=<\/td>)[\s\S]*?(?<=>)(?<NumberOfWinners>\d*?)(?=<\/td>)[\s\S]*?(?<=>)(?<MoneyPerWinner>\d*?)(?=<\/td>)";
			matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;
				WinningDetail detail = new WinningDetail();
				detail.Serial = serial;
				detail.Award = matchItem.Groups["Award"].Value;
				string strNumberOfWinners = matchItem.Groups["NumberOfWinners"].Value;
				string strMoneyPerWinner = matchItem.Groups["MoneyPerWinner"].Value;
				int numberOfWinners, moneyPerWinner;
				Int32.TryParse(strNumberOfWinners, out numberOfWinners);
				detail.NumberOfWinners = numberOfWinners;
				Int32.TryParse(strMoneyPerWinner, out moneyPerWinner);
				detail.MoneyPerWinner = moneyPerWinner;

				details.Add(detail);
			}
			model.Details = details;
			return model;
		}

		/// <summary>
		/// 从500万网站抓取七乐彩数据.
		/// </summary>
		/// <param name="issueNumber">期号.</param>
		/// <returns></returns>
		public DrawResult CrawlQiLeCaiFrom500(string issueNumber)
		{
			DrawResult model = new DrawResult();
			model.Draw = new LotteryDraw();
			model.Draw.Issuer = "福彩";
			model.Draw.Game = "七乐彩";
			model.Draw.IssueNumber = issueNumber;

			string issueNumber5Bit = issueNumber.Substring(2, 5);

			// 数据来源URL
			string detailUrl = "http://kaijiang.500.com/shtml/qlc/{0}.shtml";
			detailUrl = String.Format(detailUrl, issueNumber5Bit);

			// 下载数据.
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			// 截取开奖日期.
			string drawDatePattern = @"<span class=""span_right"">开奖日期：((\d{4})年(\d{1,2})月(\d{1,2})日) 兑奖截止日期";
			if (!Regex.IsMatch(htmlSource, drawDatePattern))
			{
				throw new Exception("未抓取到开奖日期");
			}
			var match = Regex.Match(htmlSource, drawDatePattern, RegexOptions.IgnoreCase);
			string strDrawDate = match.Groups[1].Value;
			DateTime drawDate;
			DateTime.TryParse(strDrawDate, out drawDate);
			model.Draw.DrawDate = drawDate;

			// 截取红球与篮球数据.
			string numbersPattern = @"<div class=""ball_box01"">[\s]*<ul>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_blue"">(\d{2})</li>[\s]*</ul>[\s]*</div>";
			match = Regex.Match(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			// 红球.
			model.Draw.Numbers = match.Groups.OfType<Group>().Skip(1).Take(7).Select(group => group.Value).Aggregate((a, b) => a + "," + b);
			// 篮球.
			model.Draw.BonusNumbers = match.Groups.OfType<Group>().Skip(8).Take(1).Single().Value;

			try
			{
				// 本期销量
				string amountPattern = @"<td>[\s]*本期销量：<span[\s]*class=""cfont1[\s]*"">([\S]*?)元</span>&nbsp;&nbsp;&nbsp;&nbsp;奖池滚存：<span[\s]*class=""cfont1[\s]*"">([\S]*?)元</span>";
				match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
				string strSalesAmount = match.Groups[1].Value;
				string strCumulativeAmount = match.Groups[2].Value;
				long salesAmount, cumulativeAmount;
				Int64.TryParse(strSalesAmount, NumberStyles.AllowThousands, null, out salesAmount);
				model.Draw.SalesAmount = salesAmount;
				Int64.TryParse(strCumulativeAmount, NumberStyles.AllowThousands, null, out cumulativeAmount);
				model.Draw.CumulativeAmount = cumulativeAmount;
			}
			catch
			{

			}

			string winningDetailPattern = @"<tr align=""center"">[\s]*<td>[\s]*([\S]*?)</td>[\s]*<td>[\s]*([\S]*?)</td>[\s]*<td>[\s]*([\S]*?)</td>[\s]*</tr>";
			MatchCollection matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;
				WinningDetail detail = new WinningDetail();
				detail.Serial = serial;
				detail.Award = matchItem.Groups[1].Value;
				string strNumberOfWinners = matchItem.Groups[2].Value;
				string strMoneyPerWinner = matchItem.Groups[3].Value;
				int numberOfWinners, moneyPerWinner;
				Int32.TryParse(strNumberOfWinners, out numberOfWinners);
				detail.NumberOfWinners = numberOfWinners;
				Int32.TryParse(strMoneyPerWinner, NumberStyles.AllowThousands, null, out moneyPerWinner);
				detail.MoneyPerWinner = moneyPerWinner;

				if (detail.Award == "快乐星期天特别奖" && detail.NumberOfWinners == 0)
				{
					continue;
				}

				details.Add(detail);
			}
			model.Details = details;
			return model;
		}

		/// <summary>
		/// 抓取大乐透
		/// </summary>
		/// <param name="issueNumber"></param>
		/// <returns></returns>
		public DrawResult CrawlDaLeTou(string issueNumber)
		{
			DrawResult model = new DrawResult();
			model.Draw = new LotteryDraw();
			model.Draw.Issuer = "体彩";
			model.Draw.Game = "大乐透";
			model.Draw.IssueNumber = issueNumber;

			string detailUrl = "http://www.lecai.com/lottery/draw/view/1?phase={0}";
			detailUrl = String.Format(detailUrl, issueNumber);
			WebClient webClient = new WebClient();
			string htmlSource = HttpClient.GetHtml(detailUrl, "utf-8");

			string sectionPattern = @"""{0}"":{""list"":{(?<WinningDetails>.*?)},""result"":{""red"":\[(?<RedNumbers>.*?)\],""blue"":\[(?<BlueNumbers>.*?)\],.*?},""time_draw"":""(?<DrawDate>.*?)"",(.*?),""formatSaleAmount"":""(?<SalesAmount>.*?)"",""formatPoolAmount"":""(?<CumulativeAmount>.*?)"",""fc3d_sjh"":(\S*?)}};";
			sectionPattern = sectionPattern.Replace("{", "{{").Replace("}", "}}").Replace("{{0}}", "{0}");
			sectionPattern = String.Format(sectionPattern, issueNumber);
			if (!Regex.IsMatch(htmlSource, sectionPattern))
			{
				throw new Exception("未抓取到数据");
			}
			var sectionMatch = Regex.Match(htmlSource, sectionPattern);
			string gvWinningDetails = sectionMatch.Groups["WinningDetails"].Value;
			string gvRedNumbers = sectionMatch.Groups["RedNumbers"].Value;
			string gvBlueNumbers = sectionMatch.Groups["BlueNumbers"].Value;
			string gvDrawDate = sectionMatch.Groups["DrawDate"].Value;
			string gvSalesAmount = sectionMatch.Groups["SalesAmount"].Value;
			string gvCumulativeAmount = sectionMatch.Groups["CumulativeAmount"].Value;

			if (String.IsNullOrWhiteSpace(gvRedNumbers))
			{
				throw new Exception("未抓取到数据");
			}

			DateTime drawDate;
			DateTime.TryParse(gvDrawDate, out drawDate);
			model.Draw.DrawDate = drawDate;

			string numbers = gvRedNumbers.Replace("\"", "");
			model.Draw.Numbers = numbers;

			string bonusNumbers = gvBlueNumbers.Replace("\"", "");
			model.Draw.BonusNumbers = bonusNumbers;

			long salesAmount, cumulativeAmount;
			Int64.TryParse(gvSalesAmount.Replace(",", ""), out salesAmount);
			model.Draw.SalesAmount = salesAmount;
			Int64.TryParse(gvSalesAmount.Replace(",", ""), out cumulativeAmount);
			model.Draw.CumulativeAmount = cumulativeAmount;

			string winningDetailPattern = @"""(.*?)"":{""key"":""(?<key>.*?)"",""name"":""(?<name>.*?)"",""default_prize"":""(?<default_prize>.*?)"",""usable"":(?<usable>.*?),""static_prize"":(?<static_prize>.*?),""notify"":(.*?),""bet"":""(?<bet>.*?)"",""prize"":""(?<prize>.*?)""}";
			var matches = Regex.Matches(gvWinningDetails, winningDetailPattern);
			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;
				WinningDetail detail = new WinningDetail();
				detail.Serial = serial;
				detail.Award = Regex.Unescape(matchItem.Groups["name"].Value);
				string strNumberOfWinners = matchItem.Groups["bet"].Value;
				string strMoneyPerWinner = matchItem.Groups["prize"].Value;
				int numberOfWinners, moneyPerWinner;
				Int32.TryParse(strNumberOfWinners, out numberOfWinners);
				detail.NumberOfWinners = numberOfWinners;
				Int32.TryParse(strMoneyPerWinner, out moneyPerWinner);
				detail.MoneyPerWinner = moneyPerWinner;

				details.Add(detail);
			}
			model.Details = details;
			return model;
		}

		/// <summary>
		/// 从500万网站抓取大乐透数据.
		/// </summary>
		/// <param name="issueNumber">期号.</param>
		/// <returns></returns>
		public DrawResult CrawlDaLeTouFrom500(string issueNumber)
		{
			DrawResult model = new DrawResult();
			model.Draw = new LotteryDraw();
			model.Draw.Issuer = "体彩";
			model.Draw.Game = "大乐透";
			model.Draw.IssueNumber = issueNumber;

			// 数据来源URL
			string detailUrl = "http://kaijiang.500.com/shtml/dlt/{0}.shtml";
			detailUrl = String.Format(detailUrl, issueNumber);

			// 下载数据.
			WebClient webClient = new WebClient();
			string htmlSource = webClient.DownloadString(detailUrl);

			// 截取开奖日期.
			string drawDatePattern = @"<span class=""span_right"">开奖日期：((\d{4})年(\d{1,2})月(\d{1,2})日) 兑奖截止日期";
			if (!Regex.IsMatch(htmlSource, drawDatePattern))
			{
				throw new Exception("未抓取到开奖日期");
			}
			var match = Regex.Match(htmlSource, drawDatePattern, RegexOptions.IgnoreCase);
			string strDrawDate = match.Groups[1].Value;
			DateTime drawDate;
			DateTime.TryParse(strDrawDate, out drawDate);
			model.Draw.DrawDate = drawDate;

			// 截取红球与篮球数据.
			string numbersPattern = @"<div class=""ball_box01"">[\s]*<ul>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_red"">(\d{2})</li>[\s]*<li class=""ball_blue"">(\d{2})</li>[\s]*<li class=""ball_blue"">(\d{2})</li>[\s]*</ul>[\s]*</div>";
			match = Regex.Match(htmlSource, numbersPattern, RegexOptions.IgnoreCase);
			// 红球.
			model.Draw.Numbers = match.Groups.OfType<Group>().Skip(1).Take(5).Select(group => group.Value).Aggregate((a, b) => a + "," + b);
			// 篮球.
			model.Draw.BonusNumbers = match.Groups.OfType<Group>().Skip(6).Take(2).Select(group => group.Value).Aggregate((a, b) => a + "," + b);

			try
			{
				// 本期销量
				string amountPattern = @"<td>[\s]*本期销量：<span[\s]*class=""cfont1[\s]*"">([\S]*?)元</span>&nbsp;&nbsp;&nbsp;&nbsp;奖池滚存：<span[\s]*class=""cfont1[\s]*"">([\S]*?)元</span>";
				match = Regex.Match(htmlSource, amountPattern, RegexOptions.IgnoreCase);
				string strSalesAmount = match.Groups[1].Value;
				string strCumulativeAmount = match.Groups[2].Value;
				long salesAmount, cumulativeAmount;
				Int64.TryParse(strSalesAmount, NumberStyles.AllowThousands, null, out salesAmount);
				model.Draw.SalesAmount = salesAmount;
				Int64.TryParse(strCumulativeAmount, NumberStyles.AllowThousands, null, out cumulativeAmount);
				model.Draw.CumulativeAmount = cumulativeAmount;
			}
			catch
			{

			}

			string winningDetailPattern = @"(<tr align=""center"">[\s]*<td rowspan=""2"">([\S]*?)</td>[\s]*<td>([\S]*?)</td>[\s]*<td>([\S]*)</td>[\s]*<td>([\S]*)</td>[\s]*</tr>[\s]*<tr align=""center"">[\s]*<td>([\S]*)</td>[\s]*<td>([\d]*)</td>[\s]*<td>([\S]*)</td>[\s]*</tr>)|(<tr align=""center"">[\s]*<td colspan=""2"">([\S]*?)</td>[\s]*<td>([\S]*?)</td>[\s]*<td>([\S]*?)</td>[\s]*</tr>)";
			MatchCollection matches = Regex.Matches(htmlSource, winningDetailPattern, RegexOptions.IgnoreCase);

			List<WinningDetail> details = new List<WinningDetail>();
			byte serial = 0;
			foreach (Match matchItem in matches)
			{
				serial++;

				string g2 = matchItem.Groups[2].Value;
				string g3 = matchItem.Groups[3].Value;
				string g4 = matchItem.Groups[4].Value;
				string g5 = matchItem.Groups[5].Value;
				string g6 = matchItem.Groups[6].Value;
				string g7 = matchItem.Groups[7].Value;
				string g8 = matchItem.Groups[8].Value;
				string g9 = matchItem.Groups[9].Value;
				string g10 = matchItem.Groups[10].Value;
				string g11 = matchItem.Groups[11].Value;
				string g12 = matchItem.Groups[12].Value;

				if (!String.IsNullOrWhiteSpace(g8))
				{
					// 基本&追加

					WinningDetail detail_jb = new WinningDetail();
					detail_jb.Serial = serial;
					detail_jb.Award = g2;

					string strNumberOfWinners = g4;
					string strMoneyPerWinner = g5;

					int numberOfWinners, moneyPerWinner;
					Int32.TryParse(strNumberOfWinners, NumberStyles.AllowThousands, null, out numberOfWinners);
					detail_jb.NumberOfWinners = numberOfWinners;

					// 对单注奖金进行解析.
					if (strMoneyPerWinner.Contains("基本"))
					{
						string moneyPattern = "([\\d,]*)元(基本)+([\\d,]*)元(派送)";
						var moneyMatch = Regex.Match(strMoneyPerWinner, moneyPattern);
						if (moneyMatch.Success)
						{
							string sMoney1 = moneyMatch.Groups[1].Value;
							string sMoney2 = moneyMatch.Groups[2].Value;
							
							int money1, money2;
							Int32.TryParse(sMoney1, NumberStyles.AllowThousands, null, out money1);
							Int32.TryParse(sMoney2, NumberStyles.AllowThousands, null, out money2);

							moneyPerWinner = money1 + money2;
						}
						else
						{
							moneyPerWinner = 0;
						}
					}
					else
					{
						Int32.TryParse(strMoneyPerWinner, NumberStyles.AllowThousands, null, out moneyPerWinner);
					}

					detail_jb.MoneyPerWinner = moneyPerWinner;

					details.Add(detail_jb);

					serial++;
					WinningDetail detail_zj = new WinningDetail();
					detail_zj.Serial = serial;
					detail_zj.Award = g2 + g6;

					string strNumberOfWinners_zj = g7;
					string strMoneyPerWinner_zj = g8;
					
					int numberOfWinners_zj, moneyPerWinner_zj;

					Int32.TryParse(strNumberOfWinners_zj, NumberStyles.AllowThousands, null, out numberOfWinners_zj);
					detail_zj.NumberOfWinners = numberOfWinners_zj;

					// 对单注奖金进行解析.
					if (strMoneyPerWinner_zj.Contains("基本"))
					{ 
						// 基本&派送

						string moneyPattern = "([\\d,]*)元(基本)+([\\d,]*)元(派送)";
						var moneyMatch = Regex.Match(strMoneyPerWinner_zj, moneyPattern);
						if (moneyMatch.Success)
						{
							string sMoney1 = moneyMatch.Groups[1].Value;
							string sMoney2 = moneyMatch.Groups[2].Value;
							
							int money1, money2;
							Int32.TryParse(sMoney1, NumberStyles.AllowThousands, null, out money1);
							Int32.TryParse(sMoney2, NumberStyles.AllowThousands, null, out money2);

							moneyPerWinner_zj = money1 + money2;
						}
						else
						{
							moneyPerWinner_zj = 0;
						}
					}
					else
					{
						Int32.TryParse(strMoneyPerWinner_zj, NumberStyles.AllowThousands, null, out moneyPerWinner_zj);
					}

					detail_zj.MoneyPerWinner = moneyPerWinner_zj;

					details.Add(detail_zj);
				}
				else
				{
					WinningDetail detail = new WinningDetail();
					detail.Serial = serial;
					detail.Award = g10;

					string strNumberOfWinners = g11;
					string strMoneyPerWinner = g12;
					int numberOfWinners, moneyPerWinner;
					Int32.TryParse(strNumberOfWinners, NumberStyles.AllowThousands, null, out numberOfWinners);
					detail.NumberOfWinners = numberOfWinners;
					Int32.TryParse(strMoneyPerWinner, NumberStyles.AllowThousands, null, out moneyPerWinner);
					detail.MoneyPerWinner = moneyPerWinner;

					details.Add(detail);
				}
			}
			model.Details = details;
			return model;
		}

	}
}
